﻿#region Imports
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using CommonUtilities.Enums;
using CommonUtilities.Extensions;
using CommonUtilities.Util;
using RequirementLogger.Models;
#endregion

namespace RequirementLogger.Appl.ViewModels
{
    public class MainViewModel : BaseViewModel
    {
        public MainViewModel()
        {
            // Events

            // Initializations
            DBBasePath = Application.StartupPath + @"\Database";

            try
            {
                #region Read
                string path = DBBasePath + @"\Apps.data";
                string sXML = File.ReadAllText(path);
                AllApplications = new ObservableCollection<Apps>();
                AllApplications = Serializer.DeSerializeFromFormattedData<ObservableCollection<Apps>>(
                    eSerializationType.Xml, sXML);

                // prop to dcol
                AllApplications = new ObservableCollection<Apps>();
                AllApplications = Utilities.ReadFromXML<Apps>(DBBasePath, "Apps", "data").ToObservableCollection();

                Apps.dtApps = Utilities.ConvertIEnumerableToDataTable<Apps>(AllApplications, Apps.dtApps);
                #endregion

                #region Write
                string path_wr = DBBasePath + @"\Apps_wr.data";
                string sXML_wr = Serializer.SerializeToFormattedData<ObservableCollection<Apps>>(eSerializationType.Xml, AllApplications); //File.ReadAllText(DBBasePath + @"\Apps.xml");
                File.WriteAllText(path_wr, sXML_wr);
                Process.Start("notepad", "\"" + path_wr + "\"");
                #endregion

                #region test
                SelectedApp = AllApplications.First();
                #endregion
            }
            catch (Exception ex) { ; }

            AllAppEditorProperties = new ObservableCollection<PropertyCollections>(Property.AddProperties<Apps>(AllApplications)); // .ToList().ConvertAll(prop => new ObservableCollection<Property>(prop))
        }

        private ObservableCollection<Apps> allApplications = new ObservableCollection<Apps>();
        public ObservableCollection<Apps> AllApplications
        {
            get { return allApplications; }
            set { allApplications = value; }
        }

        private ObservableCollection<PropertyCollections> allAppEditorProperties = new ObservableCollection<PropertyCollections>();
        public ObservableCollection<PropertyCollections> AllAppEditorProperties
        {
            get { return allAppEditorProperties; }
            set { allAppEditorProperties = value; }
        }

        private PropertyCollections appPropertyCollection;
        public PropertyCollections AppPropertyCollection
        {
            get { return appPropertyCollection; }
            set
            {
                appPropertyCollection = value; RaisePropertyChanged("AppPropertyCollection");

                allAppEditorProperties.Clear();
                AllAppEditorProperties.Add(appPropertyCollection);
            }
        }

        private Apps selectedApp;
        public Apps SelectedApp
        {
            get { return selectedApp; }
            set
            {
                selectedApp = value;

                if (!appPropertyCollection.IsNull()) AppPropertyCollection.Clear(); ;
                if (!selectedApp.IsNull()) AppPropertyCollection = Property.CreateProperties<Apps>(selectedApp);
                RaisePropertyChanged("AppPropertyCollection");
            }
        }

        private bool editClicked;
        public bool EditClicked
        {
            get { return editClicked; }
            set
            {
                editClicked = value; RaisePropertyChanged("EditClicked");
                if (value) { btnEdit_Click(); EditClicked = false; }
            }
        }

        private bool isAppEditorVisible;
        public bool IsAppEditorVisible
        {
            get { return isAppEditorVisible; }
            set { isAppEditorVisible = value; RaisePropertyChanged("IsAppEditorVisible "); }
        }

        private string DBBasePath;

        private bool clearClicked;
        public bool ClearClicked
        {
            get { return clearClicked; }
            set
            {
                clearClicked = value; RaisePropertyChanged("EditClicked");
                if (value) { btnClear_Click(); clearClicked = false; }
            }
        }

        private void btnEdit_Click()
        {
            if (SelectedApp.IsNull()) SelectedApp = new Apps();
            IsAppEditorVisible = true;
        }

        private void btnClear_Click()
        {
            SelectedApp = new Apps();
        }
    }
}

/*
use_json_to_populate_props_while_editing_to_remv_dependency_from_parent_object;
write_to_xml_from_datatable;
use_pkey_concept_in_datatable_as_well_as_NUD_flags; // mainly done to create identity pkeys and use of LINQ
create_icon_for_LinqDemo_app;
*/
